package com.zy.explore.sys.domain.mybatis.entity;


import com.zy.explore.common.interfaces.frm.SortFieldFrm;
import com.zy.explore.sys.interfaces.frm.ToDoItemFrm;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;

/**
 * (表to_do_item)ORM实体对象
 *
 * @author ChinaYin  主页:https://gitee.com/zy-explorej2ee 邮箱:zyexplorej2ee@sina.com
 * @date 2021年06月13日 19:22:19
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "to_do_item")
public class ToDoItem {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    private Long id;

    /**
     * 用户ID
     */
    private Integer userId;

    /**
     * 事项名称
     */
    private String itemName;

    /**
     * 事项内容
     */
    private String itemContent;

    /**
     * 计划完成时间
     */
    private Date planFinishTime;

    /**
     * 实际完成时间
     */
    private Date realFinishTime;

    /**
     * 完成状态：0.待办 1.已完成
     */
    private Integer finishStatus;

    /**
     * 删除状态：0.正常 1.已删除
     */
    private Integer delFlag;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * DTO转Example条件查询对象
     *
     * @param frm 查询参数对象
     * @return 返回Example对象
     */
    public static Example getExample(ToDoItemFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        Example example = new Example(ToDoItem.class);
        Example.Criteria criteria = example.createCriteria();
        if (frm.getId() != null) {
            criteria.andEqualTo("id", frm.getId());
        }
        if (frm.getUserId() != null) {
            criteria.andEqualTo("userId", frm.getUserId());
        }
        if (!StringUtils.isEmpty(frm.getItemName())) {
            criteria.andLike("itemName", "%" + frm.getItemName() + "%");
        }
        if (!StringUtils.isEmpty(frm.getItemContent())) {
            criteria.andLike("itemContent", "%" + frm.getItemContent() + "%");
        }
        if (!StringUtils.isEmpty(frm.getPlanFinishTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("planFinishTime", frm.getPlanFinishTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getPlanFinishTimeEnd())) {
            criteria.andLessThanOrEqualTo("planFinishTime", frm.getPlanFinishTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getRealFinishTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("realFinishTime", frm.getRealFinishTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getRealFinishTimeEnd())) {
            criteria.andLessThanOrEqualTo("realFinishTime", frm.getRealFinishTimeEnd());
        }
        if (frm.getFinishStatus() != null) {
            criteria.andEqualTo("finishStatus", frm.getFinishStatus());
        }
        if (frm.getDelFlag() != null) {
            criteria.andEqualTo("delFlag", frm.getDelFlag());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("createTime", frm.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", frm.getCreateTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("updateTime", frm.getUpdateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
            criteria.andLessThanOrEqualTo("updateTime", frm.getUpdateTimeEnd());
        }
        // 排序
        List<SortFieldFrm> sortFields = frm.getSortFields();
        if (!CollectionUtils.isEmpty(sortFields)) {
            sortFields.forEach(sortField -> {
                if (Sort.Direction.ASC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).asc();
                } else if (Sort.Direction.DESC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).desc();
                }
            });
        } else {
            // 默认按主键降序排序
            example.orderBy("id").desc();
        }
        return example;
    }

}